package com.woniuxy.provider.controller;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.woniuxy.commons.entity.Goods;
import com.woniuxy.commons.annotation.RequirePerms;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    //仪表盘只能监控 @HystrixCommand(f注解修饰的方法
    @RequirePerms("goods:all")
    @HystrixCommand(fallbackMethod = "aaa")
    @GetMapping("/all")
    public List<Goods> all(){
        //模拟服务器压力大的情形
        return Arrays.asList(
                new Goods(1001,"phone1",3000),
                new Goods(1002,"phone2",4000),
                new Goods(1003,"phone3",5000)
                );
    }
    public List<Goods> aaa(){
        //模拟服务器压力大的情形
        try{
            Thread.sleep(5000);//
        }catch (Exception e){
            e.printStackTrace();
        }
        return Arrays.asList(
                new Goods(1001,"服务器压力大",3000)
        );
    }

    @GetMapping("/find/{id}")
    public Goods find(@PathVariable(value = "id") int id){
        try{
            Thread.sleep(5000);//
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println(id);
        return new Goods(1,"袜子",100);
    }

    @PostMapping("/add")
    public boolean  add(@RequestBody Goods goods){

        System.out.println(goods);
        return true;
    }
    @PutMapping("/update")
    public void  update(@RequestBody Goods goods){
        System.out.println(goods);
    }
}
